const xlsx = require('node-xlsx').default;
const fs = require('fs')
const path = require('path')

const API_PATH = require('./utils/API_PATH');
const { post } = require('./utils/request')

// 常量
const ZXBM = '1301232005'
// 01045
// 01049
// 01056
const DXBH = '01056' || process.argv[2] // 01099

async function main() {

  // 获取平台定义数据
  async function getCfg(P, myMap) {
    await post(API_PATH.PLATFORM_CONFIG, P).then(res => {
      res.results.forEach(item => {
        myMap.set(item.num, item.name)
        myMap.set(item.num1, item.name)
      })
    }).catch(err => {
      console.log(P.type, err)
    })
  }
  // 时间统计周期
  const timeCycle = new Map()
  await getCfg({ type: "sjtjzq" }, timeCycle)
  console.log('timeCycle', timeCycle)

  // 对象定义属性类别
  const dxdy_sxlb = new Map()
  await getCfg({ type: "dxdy_sxlb" }, dxdy_sxlb)
  console.log('dxdy_sxlb', dxdy_sxlb)

  // 组件样式
  const componentStyle = new Map()
  await getCfg({ type: "componentStyle" }, componentStyle)
  console.log('componentStyle', componentStyle)

  // 对象指标统计类型
  const dxzbtjlx = new Map()
  await getCfg({ type: "dxzbtjlx" }, dxzbtjlx)
  console.log('dxzbtjlx', dxzbtjlx)

  // 对象指标空间类型
  const dxzbkjlx = new Map()
  await getCfg({ type: "dxzbkjlx" }, dxzbkjlx)
  console.log('dxzbkjlx', dxzbkjlx)

  // 属性密级
  const sxmj = new Map()
  await getCfg({ type: "sxmj" }, sxmj)
  console.log('sxmj', sxmj)

  // 登录信息
  const sxdy_dlxx = new Map()
  await getCfg({ type: "sxdy_dlxx" }, sxdy_dlxx)
  console.log('sxdy_dlxx', sxdy_dlxx)

  // componentType
  const cmpTypeJe = new Map()
  await getCfg({ type: "componentType", num2: "InputNumber" }, cmpTypeJe)
  console.log('cmpTypeJe', cmpTypeJe)

  // componentType
  const zbdw = new Map()
  await getCfg({ type: "zbdy" }, zbdw)
  console.log('zbdw', zbdw)

  // init prototype
  const initPrototype = new Map()
  await post(API_PATH.INIT_PROTOTYPE, { isShow: 1, page: 0, size: 0, syObjectNumber: " " })
    .then(res => {
      res.results.forEach(item => {
        initPrototype.set(item.fieldIdentification, item.fieldName)
      })
    })
  // console.log('initPrototype', initPrototype)

  // all prototype
  const AllPrototype = []
  await post(API_PATH.ALL_PROTOTYPE, { attributeLevel: 1, fieldName: "", isLastStage: 1, organizationNumber: ZXBM, page: 1, size: 990, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        item.attributeType = JSON.parse(item.attributeType)
        AllPrototype.push(item)
      })
    })
  // console.log('AllPrototype', AllPrototype)

  // 对象实例查询
  const ArrDxslls = []
  await post(API_PATH.MANGE_OBJECT_EXTEND, { extendClass: "dxslls", organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        item.note = JSON.parse(item.note)
        ArrDxslls.push(item)
      })
    })

  // console.log(ArrDxslls)

  // // 报表
  // const dxdybb = []
  // await post(API_PATH.MANGE_OBJECT_EXTEND, { extendClass: "dxdybb", organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
  //   .then(res => {
  //     res.results.forEach(item => {
  //       item.note = JSON.parse(item.note)
  //       dxdybb.push(item)
  //     })
  //   })

  // console.log(dxdybb)

  // 指标
  const ArrDxdyzb = []
  await post(API_PATH.MANGE_OBJECT_EXTEND, { extendClass: "dxdyzb", organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        item.note = JSON.parse(item.note)
        ArrDxdyzb.push(item)
      })
    })
  // console.log('ArrDxdyzb', ArrDxdyzb)

  // 对象定义中的事项
  const ArrDxdySx = []
  await post(API_PATH.MATTER_SUBJECT, { organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      res.results.forEach(item => {
        ArrDxdySx.push(item)
      })
    })
  // console.log('ArrDxdySx', ArrDxdySx)

  // 对象定义这个对象
  const SYObject = await post(API_PATH.MANGE_OBJECT, { organizationNumber: ZXBM, page: 1, size: 10, syObjectNumber: DXBH, })
    .then(res => {
      return res.results[0]
    })
  // console.log('SYObject', SYObject)



  // Parse a file
  const workSheetsFromFile = xlsx.parse(path.resolve(__dirname, './temp/CP-ZX-11-对象解码表_V7.0_2023.5.12.xlsx'));

  // 第一个表的列宽
  const sheetOptions = { '!cols': new Array(40).fill({ wch: 25 }) };

  // 第一个表

  const data = workSheetsFromFile[0].data
  let curCol = 0 // 第几列
  let curRow = 0 // 第几行

  data[0][0] = `${SYObject.syObjectName}对象解码表`

  curCol = 1
  curRow = 1
  data[curRow][curCol] = SYObject.syObjectName

  // TODO 
  curRow++
  data[curRow][curCol] = '业务对象' // syObjectType

  curRow++
  data[curRow][curCol] = '是'

  curRow++
  data[curRow][curCol] = '项目'

  curRow++
  data[curRow][curCol] = '数字党建'

  curRow++
  data[curRow][curCol] = '否'

  curRow++
  data[curRow][curCol] = '--'
  curRow++
  data[curRow][curCol] = '--'

  curRow++
  data[curRow][curCol] = SYObject.syObjectDesc

  data[11][1] = SYObject.syObjectName

  curRow = 11
  curCol = 2
  // 属性
  for (let i = 0; i < AllPrototype.length; i++) {
    curCol = 2

    let item = AllPrototype[i]
    let key = item.fieldIdentification.split('_').at(-1)
    data[curRow][curCol] = initPrototype.get(key) || item.fieldName

    curCol++
    data[curRow][curCol] = item.fieldName

    curCol++
    data[curRow][curCol] = `${item.dimensionClassesName}属性`

    // 属性样式
    curCol++
    data[curRow][curCol] = componentStyle.get(item.attributeType.sxys)

    // 属性样式说明
    curCol++
    data[curRow][curCol] = getStr(item.attributeType.sxys) || '--'
    function getStr(type) {
      if (type === 'input') {
        return `文本输入框。长度：${item.attributeType.maxLength}`
      }
      if (type === 'InputNumber') {
        return `数值输入框。范围：0-${item.attributeType.max}`
      }
    }

    // 属性数据来源
    curCol++
    data[curRow][curCol] = ['系统生成', '手工录入', '选择', '选择', '关联对象', '关联对象', '', '证照中心'][item.dataSource] || '--'

    // 属性数据
    curCol++
    data[curRow][curCol] = '--'

    // 是否作为实例信息模糊搜索
    curCol++
    data[curRow][curCol] = item.attributeType.sxys == 'input' ? item.dropDownAssociation == 0 ? '否' : '是' : '--'

    // 是否必填
    curCol++
    data[curRow][curCol] = item.attributeType.sfbt == 1 ? '是' : '否'

    // 是否显示
    curCol++
    data[curRow][curCol] = item.attributeType.sfxs == 1 ? '是' : '否'

    // 是否在对象画像中显示
    curCol++
    data[curRow][curCol] = item.attributeType.sfzhxzs == 1 ? '是' : '否'

    // 是否可编辑
    curCol++
    data[curRow][curCol] = item.attributeType.sfbj == 1 ? '是' : '否'

    // 算法名称
    curCol++
    data[curRow][curCol] = item.algorithmName.trim() || '--'

    // 属性描述
    curCol++
    data[curRow][curCol] = item.attributeDescription.trim() || '--'

    // 是否唯一/关键属性
    curCol++
    data[curRow][curCol] = '否' || '--'

    // 属性密级
    curCol++
    data[curRow][curCol] = item.attributeClassificationName || '一级'

    curRow++
  }

  // 对象实例流水
  curRow = 92
  curCol = 1
  for (let i = 0; i < ArrDxslls.length; i++) {
    data[curRow][curCol] = ArrDxslls[i].note.dxslmc
    data[curRow][curCol + 2] = ArrDxslls[i].note.dxslmc
    curRow++
  }

  // 事项
  curRow = 110
  curCol = 1
  for (let i = 0; i < ArrDxdySx.length; i++) {
    curCol = 1
    data[curRow][curCol++] = ArrDxdySx[i].matterSubjectName

    data[curRow][curCol++] = ['增加', '修改', '注销'][ArrDxdySx[i].userClassify]

    data[curRow][curCol++] = 0
    data[curRow][curCol++] = '--'
    data[curRow][curCol++] = '否'
    data[curRow][curCol++] = ArrDxdySx[i].matterSubjectDescription

    curRow++
  }

  // 指标
  curRow = 127
  curCol = 1
  for (let i = 0; i < ArrDxdyzb.length; i++) {
    curCol = 1
    let item = ArrDxdyzb[i].note
    // console.log(item)
    data[curRow][curCol++] = item.zbmc
    data[curRow][curCol++] = '指标'
    data[curRow][curCol++] = item.sfcwzb == 0 ? '否' : '是'
    data[curRow][curCol++] = zbdw.get(item.zbdw)
    data[curRow][curCol++] = '实例指标'
    data[curRow][curCol++] = item.sfzglsjxs == 0 ? '否' : '是'
    data[curRow][curCol++] = item.shijian.map(v => v.label).join(',')
    data[curRow][curCol++] = item.sjzqChecked.map(v => timeCycle.get(v)).join(',')
    data[curRow][curCol++] = '--'
    data[curRow][curCol++] = '--'
    data[curRow][curCol++] = item.leibie.map(v => v.label).join(',')
    data[curRow][curCol++] = dxzbtjlx.get(item.zbyxlx)
    data[curRow][curCol++] = item.tubiao.map(v => v.label).join(',') || '--'
    data[curRow][curCol++] = item.tubiao.map(v => v.sfdtzstb == 0 ? '否' : '是').join(',') || '--'
    data[curRow][curCol++] = item.tubiao.map(v => v.value).join(',') || '--'

    curRow++
  }

  ; (function test() {
    // 头部信息
    // for (let i = 1; i < 20; i++) {
    //   console.log(data[i])
    // }
    // console.log(AllPrototype[0])
    // for (let i = 12; i < 12 + AllPrototype.length; i++) {
    //   console.log(data[i])
    // }
  })();

  let buffer = xlsx.build(workSheetsFromFile.slice(0, 2), { sheetOptions });
  // console.log(workSheetsFromFile)

  fs.writeFileSync(`./build/CP-ZX-11-${SYObject.syObjectName}对象解码表_V7.0.xlsx`, buffer,)

}

main()
